package cac.fenjian.resource.WmsItemInv.rest;

import cac.fenjian.resource.WmsItemInv.service.WmsItemInvService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/WmsItemInventorySearch")
public class WmsItemInvRest {

    @Autowired
    private WmsItemInvService wmsItemInventoryService;

    @PostMapping("/searchParamsByItem")
    public PageInfo searchByPage(@RequestBody HashMap map){
        return wmsItemInventoryService.searchInventoryInfoByPage(map);
    }

    @PostMapping("searchDepartureOccupation")
    public List<HashMap> searchDepartureOccupation(@RequestBody HashMap map){
        return wmsItemInventoryService.searchDepartureOccupation(map);
    }

    @PostMapping("queryItemPallet")
    public List<HashMap> queryItemPallet(@RequestBody HashMap map){
        return wmsItemInventoryService.queryItemPallet(map);
    }

    //台账单机数量查询
    @PostMapping("/searchSingleQty")
    public ResponseMsg<QueryRespBean<HashMap>> searchSingleQty(@RequestBody Map map){
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), HashMap.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            HashMap searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<HashMap>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }
        QueryRespBean<HashMap> result = wmsItemInventoryService.searchSingleQty(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return responseMsg;
    }
}
